//
//  UserAccount.swift
//  SinaWeibo
//
//  Created by 梁楠 on 16/1/16.
//  Copyright © 2016年 QQ主流框架. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    
    //用于调用access_token，接口获取授权后的access token。
    var access_token : String?
    
    //access_token的生命周期，单位是秒数。
    var expires_in : NSTimeInterval = 0 {
        
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in  )
        }
    }
    
    //token 过期日期
    var expires_date : NSDate?
    
    //当前授权用户的UID。
    var uid : String?
    
    //用户名称
    var name : String?
    
    //用户头像地址 (大图) 180x180像素
    var avatar_large : String?
    
    init(dict : [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description : String {
        
        get {
            
            let keys = ["access_token","expires_in","expires_date","uid","name","avatar_large"]
            let dict = self.dictionaryWithValuesForKeys(keys)
            
            return dict.description
        }
    }
    
    func saveAccount () {
        
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        //将对象保存到沙盒路径中
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        
        
    }
    
    //获取磁盘中的归档对象
    class func loadUserAccount() -> UserAccount? {
        
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return account
            }
        }
        return nil
    }

    //解归档操作
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        //如果是基本数据类型, 可以直接解归档到具体类型
        expires_in = (aDecoder.decodeObjectForKey("expires_in") as? NSTimeInterval)!
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    //归档操作
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
    }
}
